package io.github.tangwan.question;

import java.lang.reflect.Field;

/**
 * @author tangwan
 * @version V1.0
 * created on 2021/8/24 11:44
 * @since JDK 1.8
 */
public class StringQuestion {

    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        question1();
        System.out.println("=================");
        question2();
        System.out.println("=================");
        question3();
    }

    /**
     * 在这中间添加N行代码,但必须保证s引用的指向不变,最终输出变成abcd
     * @throws NoSuchFieldException
     * @throws IllegalAccessException
     */
    private static void question1() throws NoSuchFieldException, IllegalAccessException {
        String s = new String("abc");
        Field value = s.getClass().getDeclaredField("value");
        value.setAccessible(true);
        value.set(s,"abcd".toCharArray());
        System.out.println(s);
    }

    /**
     * s1 == s2 false
     * s2 == s3 true
     * intern()方法先去常量池找,找到了就直接把s3引用指向常量池中地址
     */
    public static void question2() {
        String s1 = new String("abc");
        String s2 = "abc";
        System.out.println(s1 == s2);
        String s3 = s1.intern();
        System.out.println(s2 == s3);
    }

    /**
     * i1 == i2 true
     * i3 == i4 false
     * Integer内部静态的IntegerCache会缓存-128-127之间的数字
     */
    public static void question3(){
        Integer i1 = 100;
        Integer i2 = 100;
        System.out.println(i1 == i2);
        Integer i3 = 128;
        Integer i4 = 128;
        System.out.println(i3 == i4);
    }

    public static void question4(){
        StringBuffer sb = new StringBuffer();
        //sb.append()
    }

}
